<template>
  <div class="comment" ref="comment">
    <div class="comment-wrapper">
    <div class="score-wrapper">
      <div class="score">
        <div class="number">3.9</div>
        <div class="title">综合评价</div>
        <div class="desc">高于周边商家{{seller.rankRate}}%</div>
      </div>
      <div class="star">
        <div class="cell">
          <span class="title">服务态度</span>
          <v-star :score="seller.serviceScore" :size="36"></v-star>
          <span class="number">{{seller.serviceScore}}</span>
        </div>
        <div class="cell">
          <span class="title">商品评分</span>
          <v-star :score="seller.score" :size="36"></v-star>
          <span class="number">{{seller.score}}</span>
        </div>
        <div class="cell">
          <span class="title">送达时间</span>
          <span class="timer">{{seller.deliveryTime}}分钟</span>
        </div>
      </div>
    </div>
    <v-split></v-split>
    <div class="rating">
      <v-rateselect :selectType="selectType" :rateNum="rateNum" :onlyContent="onlyContent" :desc="desc"
      @setOnlyContent="setOnlyContent" @setRateType="setRateType"></v-rateselect>
      <ul class="rate-wrapper clearfix">
        <li class="rate-cell" v-for="(item,idx) in ratings" v-show="needShow(item.rateType, item.text)" :key="idx">
          <img :src="item.avatar" class="avatar">
          <div class="content-wrapper">
            <div class="time">{{item.rateTime | formatDate}}</div>
            <div class="name">{{item.username}}</div>
            <div class="star-wrapper">
              <v-star :score="item.score" :size="24"></v-star>
              <span class="duration">80分钟送达</span>
            </div>
            <div class="content">{{item.text}}</div>
            <div class="label-wrapper">
              <i class="sign" :class="item.rateType===0 ? 'icon-thumb_up': 'icon-thumb_down'"></i>
              <template v-if="item.recommend">
                <div class="label" v-for="(rec,index) in item.recommend" :key="index">{{rec}}</div>
              </template>
            </div>
          </div>
        </li>
      </ul>
    </div>
    </div>
  </div>
</template>

<script>
  import Split from '../split/Split';
  import Star from '../start/start'
  import RatingSelect from '../ratingselect/RangtingSelect'
  import {formatDate} from "../common/js/date";
  import BScroll from 'better-scroll'
  export default {
    name: "Comment",
    components: {
      'v-split': Split,
      'v-star': Star,
      'v-rateselect':RatingSelect
    },
    props:{
      seller:{
        type: Object
      }
    },
    filters:{
      formatDate(time){
        let date = new Date(time);
        return formatDate(date, 'YYYY-MM-DD hh:mm');
      }
    },
    created(){
      this.$http.get('api/ratings').then( (res)=>{
        if(res.body.status === 0) {
          this.ratings = res.body.message;
        }
      })
    },
    data(){
      return {
        selectType: 2,
        onlyContent: false,
        ratings: [],
        desc: {
          all: '全部',
          positive: '满意',
          negative: '不满意'
        }
      }
    },
    methods:{
      needShow(type, text){
        if(this.onlyContent && !text){
          return false;
        }
        if(this.selectType === 2){
          return true;
        }
        else{
          return this.selectType === type;
        }
      },
      setOnlyContent(onlyContent){
        this.$nextTick( ()=>{
          this.scroll.refresh()
        })
        this.onlyContent = onlyContent;
      },
      setRateType(type){
        this.$nextTick( ()=>{
          this.scroll.refresh()
        })
        this.selectType = type;
      }
    },
    mounted(){
      if(!this.scroll){
        this.$nextTick(()=>{
          this.scroll = new BScroll(this.$refs.comment,{
            click: true
          });
        })
      }else{
        this.$nextTick( ()=>{
          this.scroll.refresh()
        })
      }
    },
    computed:{
      rateNum(){
        let o = {};
        o.all = 0;
        o.positive = 0;
        o.negative = 0;
        if(this.ratings && this.ratings.length>0){
          this.ratings.forEach( item =>{
            if(item.rateType===0){
              o.positive++;
            }else{
              o.negative++;
            }
          });
          o.all = o.positive + o.negative;
        }
        return o;
      }
    }
    }
</script>

<style lang="scss" scoped>
  .comment{
    position: absolute;
    width: 100%;
    top: 174px;
    bottom: 0;
    left: 0;
    overflow: hidden;
    .score-wrapper {
      padding: 18px 0;
      display: flex;
      .score {
        width: 133px;
        flex: 0 0 133px;
        text-align: center;
        border-right: 1px solid $boder-color;
        .number {
          font-size: 24px;
          color: rgb(255, 153, 0);
          line-height: 28px;
          margin-bottom: 6px;
        }
        .title {
          font-size: 12px;
          color: $text-body-color;
          line-height: 18px;
          margin-bottom: 8px;
        }
        .desc {
          font-size: 10px;
          line-height: 10px;
          color: $text-desc-color;
          padding-bottom: 6px;
        }
      }
      .star {
        margin: 0 24px;
        .cell {
          margin-bottom: 8px;
          display: flex;
          .title {
            font-size: 12px;
            color: $text-body-color;
            line-height: 18px;
            margin-right: 12px;
          }
          .number{
            color: $text-waring-color;
            margin-left: 12px;
            font-size: 12px;
            line-height: 18px;
          }
          .timer{
            font-size: 12px;
            line-height: 18px;
            color: $text-desc-color;
          }
        }
      }
    }
    .rating{
      .rate-wrapper{
        .rate-cell{
          margin: 18px;
          display: flex;
          .avatar{
            height: 28px;
            width: 28px;
            flex: 0 0 28px;
            border-radius: 50%;
          }
          .content-wrapper{
            position: relative;
            margin-left: 12px;
            flex: 1;
            .time{
              position: absolute;
              right: 0;
              top: 0;
              font-size: 10px;
              font-weight: 200;
              color: $text-desc-color;
              line-height: 12px;
            }
            .name{
              font-size: 10px;
              color: $text-body-color;
              line-height: 12px;
              margin-bottom: 4px;
            }
            .star-wrapper{
              display: flex;
              .duration{
                margin-left: 6px;
                font-size: 10px;
                font-weight: 200;
                color: $text-desc-color;
                line-height: 12px;
              }
            }
            .content{
              margin-top: 6px;
              margin-bottom: 8px;
              font-size: 12px;
              color: $text-body-color;
              line-height: 18px;
            }
            .label-wrapper{
              .sign{
                line-height: 16px;
                font-size: 12px;
                &.icon-thumb_up{
                  color: $text-primary-color;
                }
                &.icon-thumb_down{
                  color: $text-gray-color;
                }
              }
              .label{
                display: inline-block;
                padding: 0 6px;
                font-size: 9px;
                line-height: 16px;
                color: $text-desc-color;
                border: 1px solid $text-desc-color;
                border-radius: 2px;
                margin-left: 8px;
                background-color: #fff;
              }
            }
          }
        }
      }
    }
  }

</style>
